define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wdsxh/member/index' + location.search,
                    add_url: 'wdsxh/member/add',
                    edit_url: 'wdsxh/member/edit',
                    del_url: 'wdsxh/member/del',
                    multi_url: 'wdsxh/member/multi',
                    import_url: 'wdsxh/member/import',
                    table: 'wdsxh_member',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'picture', title: __('Picture'), operate: 'LIKE',events: Table.api.events.image,formatter: Table.api.formatter.image},
                        {field: 'level.name', title: __('Commerce_post'), operate: 'LIKE'},
                        {field: 'company_post', title: __('Company_post'), operate: 'LIKE'},
                        {field: 'society_post', title: __('Society_post'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'jointime', title: __('入会时间'), operate: false},
                        {field: 'expiretime', title: __('到期时间'), operate: false},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on('click','#select-user',function () {
                Fast.api.open('wdsxh/member/seluser','选择用户',{
                    area:['80%','95%'],
                    callback:function(data){
                        console.log(data);
                        if(data){
                            $('#c-uid').val(data.id);
                            $('#c-user').val(data.nickname);
                        }else{
                            Layer.alert("请选择用户");
                        }
                    }
                });
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on('click','#select-user',function () {
                Fast.api.open('wdsxh/member/seluser','选择用户',{
                    area:['80%','95%'],
                    callback:function(data){
                        console.log(data);
                        if(data){
                            $('#c-uid').val(data.id);
                            $('#c-user').val(data.nickname);
                        }else{
                            Layer.alert("请选择用户");
                        }
                    }
                });
            });
        },
        seluser:function(){
            Table.api.init({
                extend: {
                }
            });
            var table = $("#table");
            table.bootstrapTable({
                url: 'wdsxh/member/seluser' + location.search,
                pk: 'id',
                sortName: 'jointime',
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                searchFormVisible: true,
                sortOrder:'desc',
                columns: [
                    [
                        {field: 'id', title: __('ID'), sortable: true,operate: false},
                        {field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: __('昵称'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'jointime', title: __('创建时间'),operate: false, formatter: Table.api.formatter.datetime, addclass: 'datetimerange', sortable: true},
                        {
                            field: 'buttons',
                            width: "80px",
                            title: __('操作'),
                            text: __('操作'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('选择'),
                                    title: __('选择'),
                                    classname: 'btn btn-xs btn-primary btn-magic btn-click',
                                    icon: 'fa fa-check',
                                    click:function (obj,row) {
                                        console.log(row);
                                        Fast.api.close(row);
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});